package com.bxw.forum.controller;


import com.bxw.forum.common.AppResult;
import com.bxw.forum.common.ResultCode;
import com.bxw.forum.exception.ApplicationException;
import com.bxw.forum.model.Article;
import com.bxw.forum.model.Board;
import com.bxw.forum.service.IBoardService;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@RequestMapping("/board")
@RestController
public class BoardController {

    @Resource
    private IBoardService boardService;
    
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;

    /**
     * 获取导航栏处的版块
     * @return
     */
    @RequestMapping("/topList")
    public AppResult<List<Board>> topList() {
        List<Board> boardList = boardService.selectByNum(indexBoardNum);

        if(boardList == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());

            return AppResult.fail(ResultCode.FAILED_PARAMS_VALIDATE);
        }


        return AppResult.success(boardList);
    }



    @RequestMapping("getById")
    public AppResult<Board> getById(@RequestParam(value = "boardId") @NonNull Long boardId) {

        Board board = boardService.selectById(boardId);

        if(board == null || board.getDeleteState() != 0) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());

            throw new ApplicationException(AppResult.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        return AppResult.success(board);
    }
}
